/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef GETBACKGROUND_H
#define GETBACKGROUND_H

#include <QObject>
#include <QQuickPaintedItem>
#include <QScreen>
#include <QGSettings/QGSettings>
#include <QQuickPaintedItem>
#include <QPainter>
#include <QDebug>
#include <QFrame>
#include <QtMath>
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
#include <QtQml/qqml.h>
#endif


//获取桌面背景的状态类
class DesktopBackground : public QQuickPaintedItem
{ 
    Q_OBJECT
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
    QML_ELEMENT
#endif
    Q_PROPERTY(QString background READ background WRITE setBackground NOTIFY backgroundChanged)
    Q_PROPERTY(QString imagestate READ imagestate WRITE setImagestate NOTIFY imageStateChanged)
    Q_PROPERTY(QString backgroundColor READ backgroundColor WRITE setbackgroundColor NOTIFY backgroundColorChanged)

public:
    explicit DesktopBackground(QQuickPaintedItem *parent = nullptr);
    Q_INVOKABLE void paint(QPainter *painter) override;

    //桌面背景图片设置
    QString background() const;
    void setBackground(const QString &background);

    //桌面背景图片拉伸方式设置
    QString imagestate() const;
    void setImagestate(const QString &imagestate);

    //桌面背景纯色背景设置
    QString backgroundColor() const;
    void setbackgroundColor(const QString &backgroundColor);

    QPixmap getBackPixmap() const;
    QPixmap getFrontPixmap() const;

Q_SIGNALS:
    //背景图片变化信号
    void backgroundChanged();

    //背景图片拉伸方式变化信号
    void imageStateChanged();

    //背景纯色变化信号
    void backgroundColorChanged();

protected:
    /**
     * 图片填充方式
     * @brief 从给定的图片中，截取一个与屏幕比例相同的矩形。(Rect,居中)。
     * @param pixmap 图片
     * @return
     */
    QRect getSourceRect(const QPixmap &pixmap);

private:
    QGSettings* m_backgroundGSettings;
    QString m_background;
    QString m_imageState;
    QString m_imageColor;

    QScreen *m_screen;
    QString m_flag;

    QPixmap m_backPixmap;
    QPixmap m_frontPixmap;
};

#endif // GETBACKGROUND_H
